import {
	token
} from '@/config/env';

function request(options) {
	const {
		isLoading
	} = options || {};
	// uni.showLoading({
	// 	title: '加载中',
	// 	mask: true
	// });
	// if (isLoading) {
	// 	uni.showLoading({
	// 		title: '加载中',
	// 		mask: true
	// 	});
	// }

	return new Promise((resolve, reject) => {
		uni.request({
			...options,

			header: {
				r: Date.parse(new Date()),
				Authorization: uni.getStorageSync('token') || token
			},

			success: res => {
				const {
					code,
					data,
					message
				} = res.data;

				switch (code) {
					case 1000:
						resolve(data);
						break;
					case 1001: 
						if (message === '登录失效，请重新登录~') {
							uni.reLaunch({
								url: '/pages/common/guide'
							})
						}
					default:
						reject(message);
				}

				resolve(data);
			},

			fail: err => {
				reject(err);
			},

			complete: () => {
				// uni.hideLoading();
			}
		});
	});
}

export {
	request
};
